-- Paste this into OpenComputer terminal to download and set up NIDAS
-- wget https://raw.githubusercontent.com/S4mpsa/NIDAS/master/setup.lua -f

local shell = require("shell")
local filesystem = require("filesystem")
local computer = require("computer")


local tarMan = "https://gitee.com/rainyYc/ocprograms/raw/master/usr/man/tar.man"
local tarBin = "https://gitee.com/rainyYc/ocprograms/raw/master/home/bin/tar.lua"

shell.setWorkingDirectory("/usr/man")
shell.execute("wget -fq " .. tarMan)
shell.setWorkingDirectory("/bin")
shell.execute("wget -fq " .. tarBin)

local NIDAS
local arg = {...}
if arg[1] == "test" or arg[1] == "latest" then
    NIDAS = "https://github.com/S4mpsa/NIDAS/releases/download/v-1/NIDAS.tar"
elseif arg[1] == "dev" or arg[1] == "staging" then
    NIDAS = "https://github.com/S4mpsa/NIDAS/releases/download/v0/NIDAS.tar"
else
    NIDAS = "https://gitee.com/rainyYc/NIDAS/raw/master/NIDAS1.tar"
end

local successful =
    pcall(
    function()
        local workDir = "/home/NIDAS/"
        local persistent = {
            "tar-exclude.txt",
            ".gitignore",
            ".github",
            "README.md",
            "settings",
            ".git",
            ".vscode"
        }
        filesystem.makeDirectory("/home/temp")
        for _, thing in ipairs(persistent) do
            if filesystem.exists(workDir .. thing) then
                shell.execute("cp -r " .. workDir .. thing .. " /home/temp/" .. thing)
            end
        end

        shell.execute("echo 1")
        
        filesystem.remove(workDir)

        shell.execute("echo 2")

        filesystem.makeDirectory(workDir)
        shell.execute("echo 3")
        shell.setWorkingDirectory(workDir)
        shell.execute("echo 4 " .. NIDAS)
        shell.execute("wget -fq " .. NIDAS)
        shell.execute("echo 5")
        shell.execute("tar -xf NIDAS1.tar")
        shell.execute("echo 6")
        filesystem.remove(workDir .. "NIDAS1.tar")
        shell.execute("echo 7")
        filesystem.copy(workDir .. ".shrc", "/home/.shrc")
        shell.execute("echo 8")
        filesystem.copy(workDir .. "setup.lua", "/home/setup.lua")
        shell.execute("echo 9")
        filesystem.makeDirectory(workDir .. "settings")
        shell.execute("echo 10")

        for _, thing in ipairs(persistent) do
            if filesystem.exists("/home/temp/" .. thing) then
                shell.execute("cp -r /home/temp/" .. thing .. " " .. workDir)
                shell.execute("echo 11 " .. thing)
            end
        end
        filesystem.remove("/home/temp/")
        shell.execute("echo 12")

        filesystem.remove("/lib/core/boot.lua")
        shell.execute("echo 13")
        filesystem.copy(workDir .. "nidas_boot.lua", "/lib/core/boot.lua")
        shell.execute("echo 14")

        filesystem.remove("/etc/profile.lua")
        shell.execute("echo 15")
        filesystem.copy(workDir .. "nidas_profile.lua", "/etc/profile.lua")
        shell.execute("echo 16")
    end
)
